package com.dddd.blog.blogsp.pipeline;

import com.dddd.blog.blogsp.entity.Blog;
import com.dddd.blog.blogsp.service.BlogContentService;
import com.overzealous.remark.Remark;
import lombok.Data;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;
import java.util.Map;


/**
 * @author 喵星球的大橘
 */
@Component
@Data
public class BlogContentPipeline implements Pipeline {

    private static final String KEY_NAME = "blog";

    @Resource
    private BlogContentService blogContentService;

    @Override
    public void process(ResultItems resultItems, Task task) {

        Map<String, Object> all = resultItems.getAll();

        if(all == null || all.size() == 0 || all.get(KEY_NAME) == null){
            return;
        }

        Blog blog = (Blog) all.get(KEY_NAME);
//        blog.setContent(convertMd(blog.getContent()));
        System.out.println(blog.getTitle());
        blogContentService.addPost(blog);
    }

    /**
     * 将html字符串转化为markdown文本
     * @param h5Content html5字符串
     * @return
     */
    private String convertMd(String h5Content){
        Remark remark = new Remark();
        return remark.convert(h5Content);
    }

}
